//
//  KongWCteatChartView.m
//  LoveOfPet
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "KongWCteatChartView.h"

#define kHeight     [UIScreen mainScreen].bounds.size.height / 667
#define kWidth      [UIScreen mainScreen].bounds.size.width / 375
@implementation KongWCteatChartView
-(void)dealloc
{
   [_array release];
    [super dealloc];
}
-(instancetype) initWithFrame:(CGRect)frame withArray:(NSMutableArray *)array andTager:(NSInteger)tager
{
    self = [super initWithFrame:frame];
    if (self) {
        _array = [array retain];
        _tager = tager;
        [self creatChart];
    }
    return self;
}

//创建表
-(void)creatChart
{
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:backView];
    [backView release];
    
    UILabel *hLine1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
    hLine1.backgroundColor = [UIColor grayColor];
    [backView addSubview:hLine1];
    [hLine1 release];
    
    UILabel *hLine2 = [[UILabel alloc] initWithFrame:CGRectMake(0, hLine1.frame.origin.y + kHeight * 61, self.frame.size.width, 1)];
    hLine2.backgroundColor = [UIColor grayColor];
    [backView addSubview:hLine2];
    [hLine2 release];
    
    UILabel *hLine3 = [[UILabel alloc] initWithFrame:CGRectMake(0, hLine2.frame.origin.y + kHeight * 61, self.frame.size.width, 1)];
    hLine3.backgroundColor = [UIColor grayColor];
    [backView addSubview:hLine3];
    [hLine3 release];
    
    UILabel *hLine4 = [[UILabel alloc] initWithFrame:CGRectMake(0, hLine3.frame.origin.y + kHeight * 61, self.frame.size.width, 1)];
    hLine4.backgroundColor = [UIColor grayColor];
    [backView addSubview:hLine4];
    [hLine4 release];
    
    UILabel *vLine1 = [[UILabel alloc] initWithFrame:CGRectMake((self.frame.size.width - kWidth * 2) / 3, hLine1.frame.origin.y + 1, 1, kHeight * 181)];
    vLine1.backgroundColor = [UIColor grayColor];
    [backView addSubview:vLine1];
    [vLine1 release];
    
    UILabel *vLine2 = [[UILabel alloc] initWithFrame:CGRectMake((self.frame.size.width - kWidth * 2) / 3 * 2, hLine1.frame.origin.y + 1, 1, kHeight * 181)];
    vLine2.backgroundColor = [UIColor grayColor];
    [backView addSubview:vLine2];
    [vLine2 release];
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in self.array) {
        [array addObject:[dic objectForKey:@"name"]];
    }
    [array addObject:@"更多"];
    if (self.array.count != 0) {

    for (int i = 0; i < 3; i ++) {
        for (int j = 0; j < 3; j ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(((self.frame.size.width - kWidth * 2) / 3 + 1) * i, (kWidth * 60 + 1) * j, (self.frame.size.width - kWidth * 2) / 3, kWidth * 60);
            [btn setTitle:[array objectAtIndex:3 * i + j] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.tag = self.tager + 3 * i + j;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [backView addSubview:btn];
        }
        }
    }
    
}

-(void)btnAction:(UIButton *)btn
{
    if (btn.tag != self.tager + 8) {
        [self.delegate returnUdi:[[self.array objectAtIndex:btn.tag - self.tager]  objectForKey:@"id"] andTitle:[[self.array objectAtIndex:btn.tag - self.tager] objectForKey:@"name"]];
    }else
    {
        [self.delegate returnTager:btn.tag];
    }
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
